<template>
    <div class="register-container">
    <div class="card">
      <h2>注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="username" required>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="password" required>
        </div>
        <div class="form-group">
          <label for="confirm-password">确认密码:</label>
          <input type="password" id="confirm-password" v-model="confirmPassword" required>
        </div>
        <div class="form-group">
          <label for="age">年龄:</label>
          <input type="number" id="age" v-model.number="age" required>
        </div>
        <div class="form-group">
          <label for="realName">真实姓名:</label>
          <input type="text" id="realName" v-model="realName" required>
        </div>
        <div class="form-group">
          <label for="email">邮箱:</label>
          <input type="email" id="email" v-model="email" required>
        </div>
        <div class="form-group">
          <label for="stuNum">学号:</label>
          <input type="text" id="stuNum" v-model="stuNum" required>
        </div>
        <div class="form-group">
          <label for="className">班级名称:</label>
          <input type="text" id="className" v-model="className" required>
        </div>
        <button type="submit">注册</button>
      </form>
      <p class="switch-link">已有账号？<router-link to="/login">立即登录</router-link></p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const username = ref('');
const password = ref('');
const confirmPassword = ref('');
const age = ref<number | null>(null);
const realName = ref('');
const email = ref('');
const stuNum = ref('');
const className = ref('');
const router = useRouter();

const handleRegister = async () => {
  if (password.value !== confirmPassword.value) {
    alert('两次输入的密码不一致！');
    return;
  }

  // 检查所有必填字段是否都已填写
  if (!username.value || !password.value || age.value === null || !realName.value || !email.value || !stuNum.value || !className.value) {
    alert('请填写所有必填信息！');
    return;
  }

  try {
    const response = await fetch('/api/demo/reg', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        username: username.value,
        pwd: password.value, // 明文密码
        age: age.value,
        realName: realName.value,
        email: email.value,
        stuNum: stuNum.value,
        className: className.value
      })
    });

    const data = await response.json();
    if (response.ok) {
      alert('注册成功！');
      router.push('/login'); // 注册成功后跳转到登录页
    } else {
      alert('注册失败: ' + (data.message || JSON.stringify(data)));
    }
  } catch (error) {
    console.error('注册请求出错:', error);
    alert('注册请求出错，请稍后再试。');
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5; /* 柔和的背景色 */
}

.card {
  background-color: #ffffff;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08); /* 更柔和的阴影 */
  max-width: 400px;
  width: 100%;
  text-align: center;
}

h2 {
  color: #333333;
  margin-bottom: 30px;
  font-size: 28px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #555555;
  font-weight: 500;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="email"] {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-sizing: border-box;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
input[type="email"]:focus {
  border-color: #6a82fb; /* 聚焦时的边框颜色 */
  outline: none;
}

button {
  width: 100%;
  padding: 12px 20px;
  background-image: linear-gradient(to right, #6a82fb, #fc5c7d); /* 渐变按钮 */
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 18px;
  font-weight: 600;
  margin-top: 20px;
  transition: opacity 0.3s ease;
}

button:hover {
  opacity: 0.9;
}

.switch-link {
  margin-top: 25px;
  font-size: 15px;
  color: #777777;
}

.switch-link router-link {
  color: #6a82fb;
  text-decoration: none;
  font-weight: 500;
}

.switch-link router-link:hover {
  text-decoration: underline;
}
</style>